<?php
namespace app\index\controller;

use app\common\Base;
use app\index\model\Business;
use app\index\model\Coupon;
use app\index\model\CouponReceive;
use app\index\model\Money;
use think\Exception;
use think\Request;

class MyMoney extends Base
{
    /**
     * @param Money $money
     * @param CouponReceive $couponReceive
     * @return \think\response\Json
     * @throws Exception
     */
    //找出用户的余额与代金券
    public function findMoneyCoupon(Request $request,Money $money,CouponReceive $couponReceive){
        // TODO 将获取缓存信息放在中间件里
        try{
            //获取请求头
            $arr = $request->cacheInfo;

            //找出余额信息
            if (empty($moneyInfo = $money->findUserMoney($arr['id']))) {
                $moneyInfo['Money'] = 0;
            }
            //找出当前用户的余额
            $userMoney = $moneyInfo['Money'];

            //找出当前用户代金券的数量
            $couponNum = $couponReceive->myMoneyCouponNum($arr['id']);

            return responseJson(Base::OK,'信息获取成功',[
                'money' => $userMoney,
                'couponNum' => $couponNum
            ]);
        }catch(Exception $e){
            throw $e;
        }
    }

    /**
     * @param CouponReceive $couponReceive
     * @return \think\response\Json
     * @throws Exception
     */
    //找出代金券信息
    public function couponInfo(Request $request,Coupon $coupon,CouponReceive $couponReceive,Business $business){
        try{
            // TODO 将获取缓存信息放在中间件里
            //获取请求头
            $arr = $request->cacheInfo;
            //找出无门槛的代金券
            $notShouldCoupon = $couponReceive->findNotShouldCoupon($arr['id']);
            //转为数组
            $arrNotShouldCoupon = json_decode($notShouldCoupon,true);
            //循环数据
            for ($i=0;$i<count($arrNotShouldCoupon);$i++){
                //找出couponId
                $couponId = $arrNotShouldCoupon[$i]['couponId'];
                //找出无门槛详细信息
                $notShouldCouponInfo = $coupon->findUserCouponInfo($couponId);
                $arrNotShouldCoupon[$i]['businessId'] = $notShouldCouponInfo['businessId'];
                $arrNotShouldCoupon[$i]['businessName'] = $business->findBusiness($notShouldCouponInfo['businessId']);
                $arrNotShouldCoupon[$i]['type'] = $notShouldCouponInfo['type'];
                if ($arrNotShouldCoupon[$i]['type'] == 0){
                    $startTime = strtotime($arrNotShouldCoupon[$i]['starTime']);
                    $endTime = strtotime($arrNotShouldCoupon[$i]['endTime']);
                    $arrNotShouldCoupon[$i]['overTime'] = self::overTime($endTime - $startTime);
            }
                if ($arrNotShouldCoupon[$i]['type'] == 1){
                    $businessData = $business->findBusinessImgName($arrNotShouldCoupon[$i]['businessId']);
                    $arrNotShouldCoupon[$i]['businessName'] = $businessData['businessName'];
                    $arrNotShouldCoupon[$i]['businessImg'] = $businessData['businessImg'];
                }
            }
            if (empty($arrNotShouldCoupon)){
                return responseJson(Base::OK,'暂无代金券信息');
            }
            return responseJson(Base::OK,'获取代金券信息成功',$arrNotShouldCoupon);
        }catch (Exception $e){
            throw $e;
        }
    }

    /**
     * @overTime 判断过期时间
     * @param $overTime
     * @return string
     */
    public function overTime($overTime){
        if ($overTime > 31104000){
            return floor($overTime / 31104000).'年过期';
        }
        if ($overTime > 2592000){
            return floor($overTime / 2592000).'月过期';
        }
        if ($overTime >= 86400){
            return floor($overTime / 86400).'天过期';
        }
        if ($overTime >= 3600){
            return floor($overTime / 3600).'小时过期';
        }
        if ($overTime >= 60){
            return floor($overTime / 60).'分过期';
        }
        return $overTime.'秒过期';
    }

}